package com.ds.home.database.dao;

import com.ds.common.JDSConstants;
import com.ds.common.logging.Log;
import com.ds.common.logging.LogFactory;
import com.ds.home.database.BaseHibernateDAO;
import com.ds.home.database.vo.DeviceEndPointVO;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.hibernate.query.NativeQuery;

import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for
 * DeviceEndPointVO entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.ds.home.database.vo.DeviceEndPointVO
 * @author MyEclipse Persistence Tools
 */
public class DeviceEndPointDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(JDSConstants.CONFIG_KEY, DeviceEndPointDAO.class);

	public static final String NAME = "name";
	public static final String IEEEADDRESS = "ieeeaddress";
	public static final String DEVICEID = "deviceId";
	
	
	public void update(DeviceEndPointVO transientInstance) {
		synchronized (transientInstance.getDeviceId()) {
			log.debug("update DeviceEndPointVO instance");
			try {
				getSession().beginTransaction();
				getSession().update(transientInstance);
				getSession().getTransaction().commit();
				log.debug("update successful");
			} catch (RuntimeException re) {
				log.error("update failed", re);
				throw re;
			}finally{
				close();
			}
		}
		
	
	}



	public  List<DeviceEndPointVO> loadByWhere(String where ){
		NativeQuery<DeviceEndPointVO> query = this.getSession().createNativeQuery("select * from ha_endpoint "+where, DeviceEndPointVO.class);
		List<DeviceEndPointVO>  volist= query.getResultList();
		return volist;
	}


	public void delete(DeviceEndPointVO persistentInstance) {
		log.debug("deleting DeviceEndPointVO instance");
		try {
			getSession().beginTransaction();
			getSession().delete(persistentInstance);
			getSession().getTransaction().commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}finally{
			close();
		}
	}

	public DeviceEndPointVO findById(String id) {
		log.debug("getting DeviceEndPointVO instance with id: " + id);
		DeviceEndPointVO instance = null;
		try {
			 instance = (DeviceEndPointVO) getSession().get(
					"com.ds.home.database.vo.DeviceEndPointVO", id);
		
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}finally{
			close();
		}
		
		return  instance ;
		
	}

	public List<DeviceEndPointVO> findByExample(DeviceEndPointVO instance) {
		log.debug("finding DeviceEndPointVO instance by example");
		List<DeviceEndPointVO> results =null;
		try {
			 results = getSession()
					.createCriteria("com.ds.home.database.vo.DeviceEndPointVO")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}finally{
			close();
		}
		return results;
	}

	public List<DeviceEndPointVO> findByProperty(String propertyName, Object value) {
		log.debug("finding DeviceEndPointVO instance with property: "
				+ propertyName + ", value: " + value);
		List queryList=null;
		try {
			String queryString = "from DeviceEndPointVO as model where model."
					+ propertyName + "= ?0";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			queryList= queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}finally{
			close();
		}
		return queryList;
		
	}

	public List<DeviceEndPointVO> findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List<DeviceEndPointVO> findByIeeeaddress(String  ieeeaddress) {
		return findByProperty(IEEEADDRESS, ieeeaddress);
	}
	public List<DeviceEndPointVO> findByDeviceId(String  deviceId) {
		return findByProperty(DEVICEID, deviceId);
	}

	public List<DeviceEndPointVO> findAll() {
		
		log.debug("finding all DeviceEndPointVO instances");
		List<DeviceEndPointVO> queryList=null;
		try {
			String queryString = "from DeviceEndPointVO";
			Query queryObject = getSession().createQuery(queryString);
			queryList= queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}finally{
			close();
		}
		return queryList;
	}
	
	
	

	public void insert(DeviceEndPointVO instance) {		
		log.debug("insert dirty DeviceEndPointVO instance");
		try {	
			 this.getSession().getTransaction().begin();
			 
			getSession().insert(instance);	
			getSession().getTransaction().commit();
			log.debug("insert successful");
		} catch (Exception re) {
			log.error("insert failed", re);
			//throw re;
		}finally{
			close();
		}
	}
	
	
}